package 中断;

public class Interrupt中断退出 {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //判断条件加上标志位
                    //获取当前线程的中断标志位
                    while (!Thread.currentThread().isInterrupted()){
                        for (int i = 0; i < 10; i++) {
                            System.out.println(i);
                            Thread.sleep(1000);
                        }
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t.start();
        //中断t线程
        Thread.sleep(3000);
        t.interrupt();
    }
}
